package cn.jiahua.service;

import cn.jiahua.dao.UserMapper;
import cn.jiahua.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Service 
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public List<User> getUserListByPage(Integer pageNo, Integer pageSize, String userName, Integer roleId) {
        return userMapper.selectUserListByName5(userName,roleId,(pageNo-1)*pageSize,pageSize);
    }

    /**
     * 事务控制
     * @param user
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addUser(User user) {
        int add = userMapper.add(user);
        return add;
    }

    @Override
    public User login(String userCode, String password) {
        User user = userMapper.selectUserByCode(userCode);
        if(user!=null){
            if(user.getUserPassword().equals(password)){
                return user;
            }
        }
        return null;
    }

    @Override
    public int totalcount(String userName, Integer userRole) {
        return userMapper.selectUserCount2(userName,userRole);
    }

    @Override
    public User viewUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public User isUserCodeExist(String userCode) {
        return userMapper.selectUserByCode(userCode);

    }

    @Override
    public int  delUserById(Integer id) {
        return userMapper.deleteUserById(id);
    }


}
